require 'spec_helper'

describe 'ceph' do
  context 'supported operating systems' do
    on_supported_os.each do |os, facts|
      context "ceph class on #{os}" do
        let :pre_condition do
          'include ::stdlib::stages
           host { "node1": ip => "192.168.1.1" }'
        end

        context "without any parameters" do
          let(:facts) do
            facts.merge({
              :fqdn  => 'node1',
              :zone => 'az1',
            })
          end

          it { is_expected.to compile.with_all_deps }
          it { is_expected.to contain_class('ceph') }

          it { is_expected.to contain_ceph__key('client.admin') }
          it { is_expected.to contain_ceph__key('mon.') }
          it { is_expected.to contain_ceph__key('client.bootstrap-mds').that_comes_before('Class[ceph::mon]') }
          it { is_expected.to contain_ceph__key('client.bootstrap-osd').that_comes_before('Class[ceph::mon]') }
          it { is_expected.to contain_ceph__key('client.bootstrap-rgw').that_comes_before('Class[ceph::mon]') }

          it { is_expected.to contain_class('ceph::params') }
          it { is_expected.to contain_class('ceph::install').that_comes_before('ceph::config') }
          it { is_expected.to contain_class('ceph::config') }

          it { is_expected.to contain_ceph__install__unit_array('ceph-disk-activate-journal@.service') }
          it { is_expected.to contain_ceph__install__unit_array('ceph-disk-activate@.service') }
          it { is_expected.to contain_ceph__install__unit_array('ceph-mon@.service') }
          it { is_expected.to contain_ceph__install__unit_array('ceph-osd@.service') }
          it { is_expected.to contain_ceph__install__unit_array('rbdmap.service') }

          it { is_expected.to contain_package('redhat-lsb-core').with_ensure('latest').that_comes_before('Package[ceph]') }
          it { is_expected.to contain_package('ceph').with_ensure('latest') }

          it { should contain_exec('Reload systemd').with_command('/usr/bin/systemctl daemon-reload') }

          it { is_expected.to contain_file('/etc/systemd/system/ceph-disk-activate-journal@.service').that_notifies('Exec[Reload systemd]') }
          it { is_expected.to contain_file('/etc/systemd/system/ceph-disk-activate@.service').that_notifies('Exec[Reload systemd]') }
          it { is_expected.to contain_file('/etc/systemd/system/ceph-mon@.service').that_notifies('Exec[Reload systemd]') }
          it { is_expected.to contain_file('/etc/systemd/system/ceph-osd@.service').that_notifies('Exec[Reload systemd]') }
          it { is_expected.to contain_file('/etc/systemd/system/rbdmap.service').that_notifies('Exec[Reload systemd]') }

          it { is_expected.to contain_file('/etc/udev/rules.d/95-ceph-osd.rules') }

          it { is_expected.to contain_file('/var/lib/ceph').with_ensure('directory') }
          it { is_expected.to contain_file('/var/lib/ceph/bootstrap-mds').with_ensure('directory') }
          it { is_expected.to contain_file('/var/lib/ceph/bootstrap-mds/ceph.keyring').with_content(File.open('spec/fixtures/files/ceph.client.bootstrap-mds.keyring').read) }
          it { is_expected.to contain_file('/var/lib/ceph/bootstrap-osd').with_ensure('directory') }
          it { is_expected.to contain_file('/var/lib/ceph/bootstrap-osd/ceph.keyring').with_content(File.open('spec/fixtures/files/ceph.client.bootstrap-osd.keyring').read) }
          it { is_expected.to contain_file('/var/lib/ceph/bootstrap-rgw').with_ensure('directory') }
          it { is_expected.to contain_file('/var/lib/ceph/bootstrap-rgw/ceph.keyring').with_content(File.open('spec/fixtures/files/ceph.client.bootstrap-rgw.keyring').read) }

          it { is_expected.to contain_file('/etc/ceph').with_ensure('directory') }
          it { is_expected.to contain_file('/etc/ceph/ceph.conf').with_content(File.open('spec/fixtures/files/ceph.conf').read) }

          it { is_expected.to contain_ceph__host('node1') }
          it { is_expected.to contain_ceph__host('node2') }
          it { is_expected.to contain_host('node1') }
          it { is_expected.to contain_host('node2') }

          context "With foundation defined host2 record" do
            let :pre_condition do
              'include ::stdlib::stages
               host { "node1": ip => "192.168.1.1" }
               host { "node2": host_aliases => ["rspec-host-alias" ], ip => "192.168.1.99" }'
            end
            it { is_expected.to contain_host('node1') }
            it { is_expected.to contain_host('node2') }
          end
          it { is_expected.to contain_ceph__mon }
          it { is_expected.to contain_file('/etc/ceph/ceph.client.admin.keyring').with_content(File.open('spec/fixtures/files/ceph.client.admin.keyring').read) }
          it { is_expected.to contain_file('/etc/ceph/ceph.mon.keyring') }

          it { is_expected.to contain_ceph__osd }
          it { is_expected.to contain_ceph__osd_disk('osd1') }
          it { should contain_exec('Ceph create osd for /dev/vda').with_command('/usr/sbin/ceph-disk-prepare --cluster ceph --cluster-uuid 50a77025-cfed-4c1c-80a2-4c6567df7924 --fs-type xfs /dev/vda && /usr/sbin/ceph-disk-activate /dev/vda1') }
          it { is_expected.to contain_ceph__osd_disk('osd2') }
          it { should contain_exec('Ceph create osd for /dev/vdb').with_command('/usr/sbin/ceph-disk-prepare --cluster ceph --cluster-uuid 50a77025-cfed-4c1c-80a2-4c6567df7924 --fs-type xfs /dev/vdb && /usr/sbin/ceph-disk-activate /dev/vdb1') }

          it { is_expected.to contain_service('rbdmap').with_ensure('running').with_enable('true') }
          it { is_expected.to contain_file_line('images/test_rbdmap').with_line('images/test id=images,keyring=/etc/ceph/ceph.client.images.keyring').that_notifies('Service[rbdmap]') }
          it { is_expected.to contain_ceph__rbd('images/test') }
          it { is_expected.to contain_file('/usr/bin/rbdmap') }
          it { is_expected.to contain_exec('ensure images/test is mapped') }
        end
        describe "with firewall" do
          context "enabled" do
            let(:facts) do
              facts.merge({
                :fqdn  => 'node1',
                :zone => 'az1',
              })
            end
            let(:params) do
              { :manage_firewall => true }
            end
            it { is_expected.to compile.with_all_deps }
            it { is_expected.to contain_class('ceph') }

            it { is_expected.to contain_firewall('100 Ceph Monitor port') }
            it { is_expected.to contain_firewall('100 Ceph OSD port range') }
          end
          context "disabled" do
            let(:facts) do
              facts.merge({
                :fqdn  => 'node1',
                :zone => 'az1',
              })
            end
            let(:params) do
              { :manage_firewall => false }
            end
            it { is_expected.to compile.with_all_deps }
            it { is_expected.to contain_class('ceph') }

            it { is_expected.to_not contain_firewall('100 Ceph Monitor port') }
            it { is_expected.to_not contain_firewall('100 Ceph OSD port range') }
          end
        end
        describe "with authkeys and rbd_devices as params" do
          context "without hiera" do
            let(:facts) do
              facts.merge({
                :fqdn  => 'node3',
                :zone => 'az1',
              })
            end

            let(:params) do
              {
                'authkeys' => {
                  'client.bootstrap-mds' => {
                    'path' => '/var/lib/ceph/bootstrap-mds/ceph.keyring',
                    'key' => 'not-in-hiera',
                    'mon' => 'allow profile bootstrap-mds',
                  },
                },
                'rbd_devices' => {
                  'images/paramtest' => {
                    'id'      => 'paramimages',
                    'keyring' => '/etc/ceph/ceph.client.paramimages.keyring',
                  },
                },
              }
            end
            it { is_expected.to compile.with_all_deps }

            it { is_expected.to contain_file('/var/lib/ceph/bootstrap-mds/ceph.keyring').with_content(File.open('spec/fixtures/files/ceph.client.bootstrap-mds-not-in-hiera.keyring').read) }
            it { is_expected.to contain_file_line('images/paramtest_rbdmap').with_line('images/paramtest id=paramimages,keyring=/etc/ceph/ceph.client.paramimages.keyring').that_notifies('Service[rbdmap]') }
            it { is_expected.to contain_ceph__rbd('images/paramtest') }
            it { is_expected.to contain_exec('ensure images/paramtest is mapped') }
          end
          context "overriding hiera" do
            let(:facts) do
              facts.merge({
                :fqdn  => 'node4',
                :zone => 'az1',
              })
            end

            let(:params) do
              {
                'authkeys' => {
                  'client.bootstrap-mds' => {
                    'path' => '/var/lib/ceph/bootstrap-mds/ceph.keyring',
                    'key' => 'not-in-hiera',
                    'mon' => 'allow profile bootstrap-mds',
                  },
                },
                'rbd_devices' => {
                  'images/paramtest' => {
                    'id'      => 'paramimages',
                    'keyring' => '/etc/ceph/ceph.client.paramimages.keyring',
                  },
                },
              }
            end
            it { is_expected.to compile.with_all_deps }

            it { is_expected.to contain_file('/var/lib/ceph/bootstrap-mds/ceph.keyring').with_content(File.open('spec/fixtures/files/ceph.client.bootstrap-mds-not-in-hiera.keyring').read) }
            it { is_expected.to contain_file_line('images/paramtest_rbdmap').with_line('images/paramtest id=paramimages,keyring=/etc/ceph/ceph.client.paramimages.keyring').that_notifies('Service[rbdmap]') }
            it { is_expected.to contain_ceph__rbd('images/paramtest') }
            it { is_expected.to contain_exec('ensure images/paramtest is mapped') }

            it { is_expected.not_to contain_file('/var/lib/ceph/bootstrap-osd/ceph.keyring') }
            it { is_expected.not_to contain_file('/var/lib/ceph/bootstrap-rgw/ceph.keyring') }
            it { is_expected.not_to contain_file_line('images/test_rbdmap') }
            it { is_expected.not_to contain_ceph__rbd('images/test') }
            it { is_expected.not_to contain_exec('ensure images/test is mapped') }
          end
        end
      end
    end
  end
end
